package com.atguigu.school.service.impl;

import com.atguigu.school.commons.MD5;
import com.atguigu.school.commons.Page;
import com.atguigu.school.mapper.TeacherMapper;
import com.atguigu.school.pojo.Teacher;
import com.atguigu.school.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    /**
     * 访问教师数据
     */
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void saveOrUpdateTeacher(Teacher teacher) {
        if (teacher.getId() == null || teacher.getId() == 0) {
            this.teacherMapper.insertTeacher(teacher);
        } else {
            //有更新密码
            if (teacher.getPassword() != null && teacher.getPassword().length() > 0) {
                teacher.setPassword(MD5.encrypt(teacher.getPassword()));
            }

            this.teacherMapper.updateTeacherById(teacher);
        }
    }

    @Override
    public void deleteTeachersByIds(List<Integer> ids) {
        this.teacherMapper.deleteTeachersByIds(ids);
    }

    @Override
    public Page<Teacher> selectTeachersByNameAndClazzName(String name, String clazzName, Integer current, Integer size) {
        Integer total = this.teacherMapper.countTeachersByNameAndClazzName(name, clazzName);
        Page<Teacher> page = new Page<>(current, size, total);

        List<Teacher> teachers = this.teacherMapper.selectTeachersByNameAndClazzName(name, clazzName, page.getOffset(), size);
        page.setRecords(teachers);
        return page;
    }


}
